package viqiang.ye.serverconsumer.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController2 {
    private Logger loggr = LoggerFactory.getLogger(this.getClass());

    /**
     * consul内置了Ribbon，所以我们还可以通过@LoadBalanced
     * 标注的RestTemplate来实现负载均衡服务调用
     */
    @Autowired
    private RestTemplate restTemplate;

    private static final String SERVER_ID = "server-provider";

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @GetMapping("hi")
    public String hello() {
        String url = "http://" + SERVER_ID + "/hello";
        return restTemplate.getForObject(url, String.class);
    }


}
